<!DOCTYPE html><html lang="zh-CN"><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Require.js | Moment.js &#x6587;&#x6863;</title>
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="shortcut icon" href="http://momentjs.cn/static/img/moment-favicon.png">
  <link rel="stylesheet" href="static/css/atom-one-light.min.css">
</head>
<body class="is-moment">
  <div class="hero hero-oneline">
    <div class="hero-centered">
      <h1>Require.js</h1>
    </div>
  </div>
  <div id="api-section-__FILENAME__">
    <div id="content" class="clearfix">
  
      <div id="column1" data-id="__ID__" class="interior">
        <header>
          <!-- <h1>Require.js</h1> -->
          <div id="gtoc">
            <ul>
              <!-- <li>v</li> -->
              <li>
                <a href="index.html#/use-it/require-js/" name="toc">&#x8FD4;&#x56DE;&#x4E0A;&#x5C42;&#x6587;&#x6863;</a>
              </li>
              <li style="border: none;">
                <a href="javascript:window.open('https://github.com/momentjscn/moment-docs-cn/edit/master/moment/00-use-it/04-require-js.md');" rel="nofollow" target="_blank">&#x63D0;&#x4EA4;&#x4FEE;&#x6539;</a>
              </li>
              <!-- <li>
                <a href="/search__SEARCH_PARAM__" name="toc">搜索</a>
              </li> -->
            </ul>
          </div>
          <hr>
        </header>

        <div id="biz_item"></div>
  
        <div id="apicontent">
          <div id="content_left">
<p>&#x5982;&#x679C;&#x4F60;&#x6253;&#x7B97;&#x5C06; moment &#x4E0E; Require.js &#x7ED3;&#x5408;&#x4F7F;&#x7528;&#xFF0C;&#x5219;&#x5F3A;&#x70C8;&#x5EFA;&#x8BAE;&#x4F60;&#x9605;&#x8BFB;<a href="javascript:window.open('http://nodejs.cn/s/BvrR2U');" target="_blank" rel="nofollow">&#x6B64;&#x5185;&#x5BB9;</a>&#x3002;
&#x53E6;&#x5916;&#xFF0C;&#x8BF7;&#x5347;&#x7EA7;&#x5230; <strong>2.14.0</strong> &#x6216;&#x66F4;&#x9AD8;&#x7248;&#x672C;&#x4EE5;&#x83B7;&#x5F97;&#x6700;&#x4F73;&#x4F53;&#x9A8C;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x901A;&#x8FC7; bower &#x6216; node_modules &#x6216;&#x5176;&#x4ED6;&#x65B9;&#x5F0F;&#x5C06; moment.js &#x4EE5;&#x53CA;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x76EE;&#x5F55;&#x653E;&#x7F6E;&#x5728;&#x57FA;&#x5C42;&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x6765;&#x83B7;&#x53D6; moment&#x3002;
&#x7136;&#x540E;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="javascript:window.open('http://nodejs.cn/s/xDgy4v');" target="_blank" rel="nofollow">adapt-pkg-main</a> &#x4E4B;&#x7C7B;&#x7684;&#x5DE5;&#x5177;&#x3001;&#x6216;&#x8005;&#x624B;&#x52A8;&#x4F7F;&#x7528;<a href="javascript:window.open('http://nodejs.cn/s/teVtRu');" target="_blank" rel="nofollow">&#x5305;&#x914D;&#x7F6E;</a>&#x3002;</p>
<!-- skip-example -->
<pre><code class="language-javascript">requirejs.config({
  packages: [{
    name: &apos;moment&apos;,
    // &#x6B64;&#x4F4D;&#x7F6E;&#x662F;&#x76F8;&#x5BF9;&#x4E8E; baseUrl &#x7684;&#x3002; 
    // &#x9009;&#x62E9; bower_components &#x8FD8;&#x662F; node_modules &#x53D6;&#x51B3;&#x4E8E;&#x5177;&#x4F53;&#x7684;&#x5B89;&#x88C5;&#x65B9;&#x5F0F;&#x3002;
    location: &apos;[bower_components|node_modules]/moment&apos;
    main: &apos;moment&apos;
  }]
});
</code></pre>
<p>&#x901A;&#x8FC7;&#x4E0A;&#x8FF0;&#x7684;&#x8BBE;&#x7F6E;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>moment</code> &#x5F15;&#x5165;&#x6838;&#x5FC3;&#x6A21;&#x5757;&#x3001;&#x4F7F;&#x7528; <code>moment/locale/de</code> &#x5F15;&#x5165; <code>de</code> &#x8BED;&#x8A00;&#x73AF;&#x5883;&#x3002;</p>
<!-- skip-example -->
<pre><code class="language-javascript">// &#x53EA;&#x9700;&#x8981;&#x6838;&#x5FC3;&#x6A21;&#x5757;&#x3002;
define([&apos;moment&apos;], function (moment) {
    console.log(moment().format(&apos;LLLL&apos;));  // &apos;Friday, June 24, 2016 1:42 AM&apos;
});

// &#x5177;&#x6709;&#x5355;&#x4E00;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x7684;&#x6838;&#x5FC3;&#x6A21;&#x5757;&#x3002;
define([&apos;moment&apos;, &apos;moment/locale/de&apos;], function (moment) {
    moment.locale(&apos;de&apos;);
    console.log(moment().format(&apos;LLLL&apos;)); // &apos;Freitag, 24. Juni 2016 01:42&apos;
});

// &#x5177;&#x6709;&#x6240;&#x6709;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x7684;&#x6838;&#x5FC3;&#x6A21;&#x5757;&#x3002;
define([&apos;moment/min/moment-with-locales&apos;], function (moment) {
    moment.locale(&apos;de&apos;);
    console.log(moment().format(&apos;LLLL&apos;)); // &apos;Freitag, 24. Juni 2016 01:42&apos;
});

// &#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x3002;
define([&apos;require&apos;, &apos;moment&apos;], function(require, moment) {
  // &#x68C0;&#x6D4B;&#x5230;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x4E4B;&#x540E;&#xFF0C;&#x5728;&#x67D0;&#x4E2A;&#x6A21;&#x5757;&#x5185;&#x90E8;&#x3002; 
  // &#x8FD9;&#x662F;&#x5728;&#x6A21;&#x5757;&#x52A0;&#x8F7D;&#x65F6;&#x95F4;&#x4E4B;&#x524D;&#x4E0D;&#x77E5;&#x9053;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x7684;&#x60C5;&#x51B5;&#x3002;
  require([&apos;moment/locale/de&apos;], function(localeModule) {
    // &#x6B64;&#x5904;&#x5DF2;&#x52A0;&#x8F7D;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#xFF0C;&#x4F46;&#x5C1A;&#x672A;&#x4F7F;&#x7528;&#x3002;
    console.log(moment().format(&apos;LLLL&apos;));  // &apos;Friday, June 24, 2016 1:42 AM&apos;

    moment.locale(&apos;de&apos;);
    // &#x5DF2;&#x7ECF;&#x6B63;&#x786E;&#x5730;&#x8BBE;&#x7F6E;&#x4E86;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x4E4B;&#x540E;&#x4F7F;&#x7528; moment&#x3002;
    console.log(moment().format(&apos;LLLL&apos;)); // &apos;Freitag, 24. Juni 2016 01:42&apos;
  })
});
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x66F4;&#x590D;&#x6742;&#x7684;&#x7528;&#x4F8B;&#xFF0C;&#x8BF7;&#x9605;&#x8BFB; <a href="javascript:window.open('http://nodejs.cn/s/BvrR2U');" target="_blank" rel="nofollow">@jrburke &#x7684;&#x51FA;&#x8272;&#x89E3;&#x91CA;</a>&#x3002;</p>
<p>Moment &#x4ECD;&#x5C06;&#x4F1A;&#x521B;&#x5EFA;&#x5168;&#x5C40;&#x7684; <code>moment</code>&#xFF0C;&#x8FD9;&#x5BF9;&#x4E8E;&#x63D2;&#x4EF6;&#x548C;&#x5176;&#x4ED6;&#x7B2C;&#x4E09;&#x65B9;&#x4EE3;&#x7801;&#x5F88;&#x6709;&#x7528;&#x3002;
&#x5982;&#x679C;&#x9700;&#x8981;&#x5236;&#x6B62;&#x8BE5;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x5219;&#x5728;&#x6A21;&#x5757;&#x914D;&#x7F6E;&#x4E0A;&#x4F7F;&#x7528; <code>noGlobal</code> &#x9009;&#x9879;&#x3002;</p>
<!-- skip-example -->
<pre><code class="language-javascript">require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
</code></pre>
<p>&#x5982;&#x679C;&#x672A;&#x6307;&#x5B9A; <code>noGlobal</code>&#xFF0C;&#x5219;&#x5168;&#x5C40;&#x5BFC;&#x51FA;&#x7684; moment &#x5C06;&#x4F1A;&#x6253;&#x5370;&#x5F03;&#x7528;&#x8B66;&#x544A;&#x3002;
&#x4ECE;&#x4E0B;&#x4E00;&#x4E2A;&#x4E3B;&#x8981;&#x7248;&#x672C;&#x5F00;&#x59CB;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x8FD9;&#x79CD;&#x884C;&#x4E3A;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x81EA;&#x884C;&#x5BFC;&#x51FA;&#x3002;</p>
<p>&#x5BF9;&#x4E8E; <strong>2.5.x</strong> &#x7248;&#x672C;&#xFF0C;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x4F9D;&#x8D56; Moment &#x4F46;&#x4E0E; AMD &#x4E0D;&#x517C;&#x5BB9;&#x7684;&#x5176;&#x4ED6;&#x63D2;&#x4EF6;&#xFF0C;&#x5219;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5728; r.js &#x914D;&#x7F6E;&#x4E2D;&#x6DFB;&#x52A0; <a href="javascript:window.open('http://nodejs.cn/s/jMqX5o');" target="_blank" rel="nofollow"><code>wrapShim: true</code></a>&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x4F7F; moment.js &#x63D2;&#x4EF6;&#x53EF;&#x4EE5;&#x5728; requirejs &#x73AF;&#x5883;&#x4E2D;&#x88AB;&#x52A0;&#x8F7D;&#xFF0C;moment &#x4F1A;&#x88AB;&#x521B;&#x5EFA;&#x4E3A;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x6A21;&#x5757;&#x3002;
&#x56E0;&#x6B64;&#xFF0C;&#x5FC5;&#x987B;&#x4F7F;&#x7528; <code>paths</code> &#x6765;&#x786E;&#x5B9A;&#x76EE;&#x5F55;&#xFF0C;&#x5C06; moment &#x5B8C;&#x5168;&#x6309; <code>&quot;moment&quot;</code> &#x52A0;&#x8F7D;&#x3002;
&#x4F7F;&#x7528; <code>&quot;vendor\moment&quot;</code> &#x4E4B;&#x7C7B;&#x7684;&#x8DEF;&#x5F84;&#x5F15;&#x5165; moment &#x5C06;&#x4F1A;&#x8FD4;&#x56DE; <code>undefined</code>&#x3002;</p>
<p>&#x4ECE; <strong>2.9.0</strong> &#x7248;&#x672C;&#x5F00;&#x59CB;&#xFF0C;moment &#x4F1A;&#x5C06;&#x81EA;&#x8EAB;&#x5BFC;&#x51FA;&#x4E3A;&#x533F;&#x540D;&#x6A21;&#x5757;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x5982;&#x679C;&#x4EC5;&#x4F7F;&#x7528;&#x6838;&#x5FC3;&#x6A21;&#x5757;&#xFF08;&#x4E0D;&#x4F7F;&#x7528;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x6216;&#x63D2;&#x4EF6;&#xFF09;&#xFF0C;&#x5219;&#x5C06;&#x5176;&#x653E;&#x7F6E;&#x5728;&#x975E;&#x6807;&#x51C6;&#x4F4D;&#x7F6E;&#x65F6;&#x4E0D;&#x9700;&#x8981;&#x914D;&#x7F6E;&#x3002;</p>
					</div>
          <div id="content_right">
<p>We strongly recommend reading
<a href="javascript:window.open('https://github.com/requirejs/requirejs/issues/1554#issuecomment-226269905');" target="_blank" rel="nofollow">this</a>
if you plan to use moment with Require.js. Also upgrade to <strong>2.14.0</strong> or above
for best experience.</p>
<p>As a start, you might have aquired moment through bower or node_modules or
anything else that places moment.js together with a locales directory in a base
folder. Then you should use a tool like
<a href="javascript:window.open('https://github.com/jrburke/adapt-pkg-main');" target="_blank" rel="nofollow">adapt-pkg-main</a>, or manually --
using <a href="javascript:window.open('http://requirejs.org/docs/api.html#packages');" target="_blank" rel="nofollow">packages config</a>.</p>
<!-- skip-example -->
<pre><code class="language-javascript">requirejs.config({
  packages: [{
    name: &apos;moment&apos;,
    // This location is relative to baseUrl. Choose bower_components
    // or node_modules, depending on how moment was installed.
    location: &apos;[bower_components|node_modules]/moment&apos;
    main: &apos;moment&apos;
  }]
});
</code></pre>
<p>With the above setup, you can require the core with <code>moment</code> and <code>de</code> locale
with <code>moment/locale/de</code>.</p>
<!-- skip-example -->
<pre><code class="language-javascript">// only needing core
define([&apos;moment&apos;], function (moment) {
    console.log(moment().format(&apos;LLLL&apos;));  // &apos;Friday, June 24, 2016 1:42 AM&apos;
});

// core with single locale
define([&apos;moment&apos;, &apos;moment/locale/de&apos;], function (moment) {
    moment.locale(&apos;de&apos;);
    console.log(moment().format(&apos;LLLL&apos;)); // &apos;Freitag, 24. Juni 2016 01:42&apos;
});

// core with all locales
define([&apos;moment/min/moment-with-locales&apos;], function (moment) {
    moment.locale(&apos;de&apos;);
    console.log(moment().format(&apos;LLLL&apos;)); // &apos;Freitag, 24. Juni 2016 01:42&apos;
});

// async load locale
define([&apos;require&apos;, &apos;moment&apos;], function(require, moment) {
  // Inside some module after the locale is detected. This is the
  // case where the locale is not known before module load time.
  require([&apos;moment/locale/de&apos;], function(localeModule) {
    // here the locale is loaded, but not yet in use
    console.log(moment().format(&apos;LLLL&apos;));  // &apos;Friday, June 24, 2016 1:42 AM&apos;

    moment.locale(&apos;de&apos;);
    // Use moment now that the locale has been properly set.
    console.log(moment().format(&apos;LLLL&apos;)); // &apos;Freitag, 24. Juni 2016 01:42&apos;
  })
});
</code></pre>
<p>For more complicated use cases please read <a href="javascript:window.open('https://github.com/requirejs/requirejs/issues/1554#issuecomment-226269905');" target="_blank" rel="nofollow">excellent explanation by @jrburke</a>.</p>
<p>Moment will still create a <code>moment</code> global, which is useful to plugins and other third-party code. If you wish to squash that global, use the <code>noGlobal</code> option on the module config.</p>
<!-- skip-example -->
<pre><code class="language-javascript">require.config({
    config: {
        moment: {
            noGlobal: true
        }
    }
});
</code></pre>
<p>If you don&apos;t specify <code>noGlobal</code> then the globally exported moment will print
a deprecation warning. From next major release you&apos;ll have to export it
yourself if you want that behavior.</p>
<p>For version <strong>2.5.x</strong>, in case you use other plugins that rely on Moment but are
not AMD-compatible you may need to add <a href="javascript:window.open('https://github.com/jrburke/r.js/blob/b8a6982d2923ae8389355edaa50d2b7f8065a01a/build/example.build.js#L68-L78');" target="_blank" rel="nofollow"><code>wrapShim: true</code></a>
to your r.js config.</p>
<p><strong>Note:</strong> To allow moment.js plugins to be loaded in requirejs environments, moment is created as a named module. Because of this, moment <strong>must</strong> be loaded exactly as as <code>&quot;moment&quot;</code>, using <code>paths</code> to determine the directory. Requiring moment with a path like <code>&quot;vendor\moment&quot;</code> will return <code>undefined</code>.</p>
<p><strong>Note:</strong> From version <strong>2.9.0</strong> moment exports itself as an anonymous module,
so if you&apos;re using only the core (no locales / plugins), then you don&apos;t need
config if you put it on a non-standard location.</p>
					</div>
          <div></div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/javascript.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?7a7f78c805fbacc7e075d34f153031aa"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>

  <style>
    .hero-centered {
      margin: 0;
          margin-left: 10px;
    }
#gtoc ul {
  list-style: none;
  margin-left: 0;
  line-height: 1.5rem;
  padding: 0;
}
#gtoc>ul>li {
  display: inline;
  border-right: 1px solid #000;
  margin-right: .4rem;
  padding-right: .4rem;
}
#gtoc>ul>li>a {
  color: #61b2a7;
}
#content_left {
  width: 49%;float: left;border-right: 1px solid #eee;    padding-right: 2%;
}
#content_right {
float: left;width: 48%;margin-left: 3%;
}
#column1 {
  padding: 0 2rem;
}
  </style>


</body></html>